﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSS.hameduconn.Business.Dto;

namespace WSS.hameduconn.Business.Api
{
    public class RegistrationsApi
    {
        private static WSShameduconnDBEntities contextDB = new WSShameduconnDBEntities();

        public static List<RegistrationsDto> GetRegistrationList()
        {
            try
            {
                var registrations = contextDB.Registrations.ToList();
                List<RegistrationsDto> lstRegistrations = new List<RegistrationsDto>();
                foreach (var obj in registrations)
                {
                    RegistrationsDto registration = new RegistrationsDto()
                    {
                        Id = obj.Id,
                        Username = obj.Username,
                        Password = obj.Password,
                        ConfirmPassword = obj.ConfirmPassword,
                        FirstName = obj.FirstName,
                        LastName = obj.LastName,
                        Email = obj.Email,
                        Street = obj.Street,
                        City = obj.City,
                        State = obj.State,
                        ZipCode = obj.ZipCode,
                        CreditCardNo = obj.CreditCardNo,
                        //ExpirationDate = !string.IsNullOrEmpty(obj.ExpirationDate.ToString()) ? DateTime.Parse(obj.ExpirationDate.ToString()) ,
                        CVS = obj.CVS
                    };
                    if (!string.IsNullOrEmpty(obj.ExpirationDate.ToString()))
                    {
                        registration.ExpirationDate = DateTime.Parse(obj.ExpirationDate.ToString());
                    }
                    lstRegistrations.Add(registration);
                }
                return lstRegistrations;
            }
            catch (Exception ex) { return new List<RegistrationsDto>(); }
        }
        public static RegistrationsDto Inserted(RegistrationsDto registration)
        {
            try
            {
                Registration obj = new Registration()
                {
                    Username = registration.Username,
                    Password = registration.Password,
                    ConfirmPassword = registration.ConfirmPassword,
                    FirstName = registration.FirstName,
                    LastName = registration.LastName,
                    Email = registration.Email,
                    Street = registration.Street,
                    State = registration.State,
                    ZipCode = registration.ZipCode,
                    CreditCardNo = registration.CreditCardNo,
                    ExpirationDate = DateTime.Now,
                    CVS = registration.CVS,
                    City = registration.City
                };
                contextDB.Registrations.Add(obj);
                contextDB.SaveChanges();
                return registration;
            }
            catch (Exception ex) { throw; }
        }

        public static void Updated(RegistrationsDto registration)
        {
            try
            {
                Registration objRegistration = contextDB.Registrations.First(att => att.Id == registration.Id);
                objRegistration.Username = registration.Username;
                objRegistration.Password = registration.Password;
                objRegistration.ConfirmPassword = registration.ConfirmPassword;
                objRegistration.FirstName = registration.FirstName;
                objRegistration.LastName = registration.LastName;
                objRegistration.Email = registration.Email;
                objRegistration.Street = registration.Street;
                objRegistration.State = registration.State;
                objRegistration.ZipCode = registration.ZipCode;
                objRegistration.CreditCardNo = registration.CreditCardNo;
                objRegistration.ExpirationDate = registration.ExpirationDate;
                objRegistration.CVS = registration.CVS;
                objRegistration.City = registration.City;
                contextDB.SaveChanges();
            }
            catch (Exception ex) { }
        }

        public static void Deleted(RegistrationsDto registration)
        {
            try
            {
                Registration objRegistration = contextDB.Registrations.First(i => i.Id == registration.Id);
                contextDB.Registrations.Remove(objRegistration);
                contextDB.SaveChanges();
            }
            catch (Exception ex) {  }
        }

        public static RegistrationsDto GetRegistrationById(int id)
        {
            try
            {
                Registration objRegistration = contextDB.Registrations.First(i => i.Id == id);
                RegistrationsDto registration = new RegistrationsDto();
                registration.Id = objRegistration.Id;
                registration.Username = objRegistration.Username;
                registration.Password = objRegistration.Password;
                registration.ConfirmPassword = objRegistration.ConfirmPassword;
                registration.FirstName = objRegistration.FirstName;
                registration.LastName = objRegistration.LastName;
                registration.Email = objRegistration.Email;
                registration.Street = objRegistration.Street;
                registration.State = objRegistration.State;
                registration.ZipCode = objRegistration.ZipCode;
                registration.CreditCardNo = objRegistration.CreditCardNo;
                if(!string.IsNullOrEmpty(objRegistration.ExpirationDate.ToString())){
                    registration.ExpirationDate = DateTime.Parse(objRegistration.ExpirationDate.ToString());
                }
                //registration.ExpirationDate = (objRegistration.ExpirationDate != null) ?DateTime.Parse(objRegistration.ExpirationDate.ToString());
                registration.CVS = objRegistration.CVS;
                registration.City = objRegistration.City;
                return registration;
            }
            catch (Exception ex) { return new RegistrationsDto(); }
        }
    }
}
